package com.risk.login.controller;

import com.risk.login.config.LoginStaticConfig;
import com.risk.login.dao.po.LoginUserPo;
import com.risk.login.dto.LoginUserDto;
import com.risk.login.dto.common.LoginConstant;
import com.risk.login.exception.BusinessException;
import com.risk.login.service.LoginUserRService;
import com.risk.login.service.LoginUserWService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Desc
 * @Date 2018/7/2:下午9:43
 * @Author yzMa
 * @email 2641007740@qq.com
 */
@RestController
@RequestMapping("/h5/login")
public class H5LoginController {


    @Autowired
    private LoginUserWService loginUserWService;

    @Autowired
    private LoginUserRService loginUserRService;

    @Autowired
    private LoginStaticConfig loginStaticConfig;

    /**
     *
     * @param model
     * @return
     */
    @GetMapping("/page")
    public ModelAndView loginPage(Model model){
        model.addAttribute("resources",loginStaticConfig.getDomain());
        return new ModelAndView("login-page");
    }

    /**
     *
     * @return
     */
    @GetMapping("/index/success")
    public ModelAndView success(){
        return new ModelAndView("login-success");
    }

    /**
     * 给zuul
     * @param loginToken
     * @return
     */
    @PostMapping("/update/{loginToken}/ttl")
    private boolean updateTokenTTL(@PathVariable("loginToken") String loginToken){
        return loginUserWService.updateLoginTokenTTL(loginToken,LoginConstant.EXPIRE_TIME_SECONDS);
    }


    /**
     * 给zuul
     * @param loginToken
     * @return
     */
    @GetMapping("/check/{loginToken}")
    public LoginUserDto checkLoginByToken(@PathVariable("loginToken") String loginToken){
        return loginUserRService.checkLoginedByToken(loginToken);
    }

    /**
     *
     * @param phone
     * @param passwd
     * @return
     * @throws BusinessException
     */
    @PostMapping("/do")
    public void doLogin(@RequestParam String phone, @RequestParam String passwd, @RequestParam(required = false) String targetUrl, HttpServletResponse resp) throws BusinessException, IOException {
        LoginUserDto loginUserDto = loginUserRService.checkPhone(phone, passwd);

        String loginTokenWithoutTimeStamp = loginUserWService.writeLoginInfo(loginUserDto);
        String cookieVal = loginTokenWithoutTimeStamp+":"+System.currentTimeMillis();

        Cookie cookie = new Cookie(LoginConstant.LOGIN_COOKIE_NAME,cookieVal);
//        cookie.setDomain("gomaoxian.com");
        cookie.setMaxAge(-1);
        cookie.setPath("/");
        resp.addCookie(cookie);

        resp.sendRedirect(loginStaticConfig.getDefaultSuccess());

    }
}
